package com.beta.cust.bind.config;

import com.beta.cat.config.BaseRedisConfig;
import com.beta.cat.service.impl.RedissonServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * Redis自动装配
 */
@Slf4j
@Configuration
public class RedisConfig extends BaseRedisConfig {

    @Autowired
    Environment environment;

    @PostConstruct
    public void init() {
        System.out.printf("RedisConfig.init");
    }

    @PreDestroy
    public void destroy() {
        System.out.printf("RedisConfig.destroy");
    }

    @Bean
    public RedissonServiceImpl initRedissonService() {
        Config config = initConfig(environment, "spring.redis.single");
        if (null == config) {
            log.error("RedisConfig.initRedissonService err config is null!");
            return null;
        }

        RedissonClient redissonClient = Redisson.create(config);
        RedissonServiceImpl redissonService = new RedissonServiceImpl();
        try {
            redissonService.setRedissonClient(redissonClient);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return redissonService;
    }
}
